/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.sadhar.sipp.perekamanrapatkerja;

import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zul.Button;
import org.zkoss.zul.Hbox;
import org.zkoss.zul.Label;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Row;
import org.zkoss.zul.RowRenderer;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Toolbarbutton;
import org.zkoss.zul.Window;

/**
 *
 * @author hendro
 */
public class RapatKerjaRowRenderer implements RowRenderer {

    private PerekamanRapatKerjaDAO rapatKerjaDAO = new PerekamanRapatKerjaDAOImpl();
    public int no = 1;

    public void render(final Row row, Object data) throws Exception {
        final PerekamanRapatKerja rapat = (PerekamanRapatKerja) data;
        new Label(String.valueOf(no++)).setParent(row);
        new Label(rapat.getNamaRapatKerja()).setParent(row);

        Toolbarbutton toolbarbtnUbah = new Toolbarbutton();
        toolbarbtnUbah.setLabel("[ubah]");
        toolbarbtnUbah.addEventListener("onClick", new EventListener() {

            public void onEvent(Event arg0) throws Exception {
                Window window = (Window) row.getRoot().getFellow("contentDiv").getFirstChild();
                Textbox txtboxNamaRapatKerja = (Textbox) window.getFellow("txtboxNamaRapatKerja");
                Textbox txtboxKodeRapatKerja = (Textbox) window.getFellow("txtboxKodeRapatKerja");
                txtboxKodeRapatKerja.setValue(rapat.getKodeRapatKerja()+"");
                txtboxNamaRapatKerja.setValue(rapat.getNamaRapatKerja());
            }
        });
        //lblUbah.setParent(row);

        final Toolbarbutton toolbarbtnHapus = new Toolbarbutton();
        toolbarbtnHapus.setLabel("[hapus]");
        toolbarbtnHapus.addEventListener("onClick", new EventListener() {

            public void onEvent(Event arg0) throws Exception {
                if (Messagebox.show("Apakah data Rapat Kerja: " + rapat.getNamaRapatKerja() + " akan dihapus?", "Konfirmasi", Messagebox.YES | Messagebox.NO, null) == Messagebox.YES) {
                    try {
                        rapatKerjaDAO.delete(rapat);
                    } catch (Exception ex) {
                        Messagebox.show("Gagal menghapus data: " + ex.getMessage());
                    }
                    Window window = (Window) row.getRoot().getFellow("contentDiv").getFirstChild();
                    Button btnBatal = (Button) window.getFellow("btnBatal");
                    toolbarbtnHapus.addForward(null, btnBatal, "onClick");
                }
            }
        });
        //lblUbah.setParent(row);

        Hbox box = new Hbox();
        box.appendChild(toolbarbtnUbah);
    box.appendChild(toolbarbtnHapus);
        box.setParent(row);
    }
}
